package vip.xiaonuo.supplier.modular.business.businessdetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhs.core.trans.anno.Trans;
import com.fhs.trans.service.impl.TransService;
import jakarta.annotation.Resource;
import org.glassfish.jaxb.core.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.base.productions.entity.SupplierProductions;
import vip.xiaonuo.supplier.modular.base.productions.service.SupplierProductionsService;
import vip.xiaonuo.supplier.modular.business.businessMain.entity.SupplierBusinessMain;
import vip.xiaonuo.supplier.modular.business.businessdetail.entity.SupplierBusinessDetail;
import vip.xiaonuo.supplier.modular.business.businessdetail.mapper.SupplierBusinessDetailMapper;
import vip.xiaonuo.supplier.modular.business.businessdetail.param.SupplierBusinessDetailAddParam;
import vip.xiaonuo.supplier.modular.business.businessdetail.param.SupplierBusinessDetailEditParam;
import vip.xiaonuo.supplier.modular.business.businessdetail.param.SupplierBusinessDetailIdParam;
import vip.xiaonuo.supplier.modular.business.businessdetail.param.SupplierBusinessDetailPageParam;
import vip.xiaonuo.supplier.modular.business.businessdetail.service.SupplierBusinessDetailService;
import vip.xiaonuo.supplier.modular.custom.objects.entity.SupplierObjects;
import vip.xiaonuo.supplier.modular.custom.objects.mapper.SupplierObjectsMapper;
import vip.xiaonuo.supplier.modular.finance.account.entity.SupplierAccount;
import vip.xiaonuo.supplier.modular.finance.account.service.SupplierAccountService;
import vip.xiaonuo.supplier.modular.finance.settlement.entity.SupplierSettlement;
import vip.xiaonuo.supplier.modular.finance.settlement.param.SupplierSettlementAddParam;
import vip.xiaonuo.supplier.modular.finance.settlement.service.SupplierSettlementService;
import vip.xiaonuo.supplier.modular.finance.settlement.service.impl.SupplierSettlementServiceImpl;
import vip.xiaonuo.supplier.modular.finance.settlementproject.entity.SupplierSettlementProject;
import vip.xiaonuo.supplier.modular.finance.settlementproject.service.SupplierSettlementProjectService;
import vip.xiaonuo.supplier.modular.warehouse.stocks.entity.SupplierStocks;
import vip.xiaonuo.supplier.modular.warehouse.stocks.service.SupplierStocksService;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.entity.SupplierStoresStream;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.service.SupplierStoresStreamService;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 业务明细表Service接口实现类
 *
 * @author Mr.wu
 * @date 2025/03/26 11:23
 **/
@Service
public class SupplierBusinessDetailServiceImpl extends ServiceImpl<SupplierBusinessDetailMapper, SupplierBusinessDetail> implements SupplierBusinessDetailService {
    @Resource
    private TransService transService;
    @Resource
    private SupplierStoresStreamService supplierStoresStreamService;
    @Resource
    private SupplierStocksService supplierStocksService;
    @Resource
    private SupplierProductionsService supplierProductionsService;
    @Resource
    private SupplierSettlementProjectService supplierSettlementProjectService;

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    private static final AtomicInteger sequence = new AtomicInteger(0);

    private static String currentMinute = "";


    @Override
    public Page<SupplierBusinessDetail> page(SupplierBusinessDetailPageParam supplierBusinessDetailPageParam) {
        QueryWrapper<SupplierBusinessDetail> queryWrapper = new QueryWrapper<SupplierBusinessDetail>().checkSqlInjection();
        if (ObjectUtil.isNotEmpty(supplierBusinessDetailPageParam.getDetailBusinessId())) {
            queryWrapper.lambda().eq(SupplierBusinessDetail::getDetailBusinessId, supplierBusinessDetailPageParam.getDetailBusinessId());
        }
        if (ObjectUtil.isNotEmpty(supplierBusinessDetailPageParam.getDetailProductionId())) {
            queryWrapper.lambda().eq(SupplierBusinessDetail::getDetailProductionId, supplierBusinessDetailPageParam.getDetailProductionId());
        }
        if (ObjectUtil.isNotEmpty(supplierBusinessDetailPageParam.getDetailShippingStorage())) {
            queryWrapper.lambda().eq(SupplierBusinessDetail::getDetailShippingStorage, supplierBusinessDetailPageParam.getDetailShippingStorage());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierBusinessDetail::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if (ObjectUtil.isAllNotEmpty(supplierBusinessDetailPageParam.getSortField(), supplierBusinessDetailPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierBusinessDetailPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierBusinessDetailPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierBusinessDetailPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierBusinessDetail::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierBusinessDetailAddParam supplierBusinessDetailAddParam) {
        SupplierBusinessDetail supplierBusinessDetail = BeanUtil.toBean(supplierBusinessDetailAddParam, SupplierBusinessDetail.class);
        //数据归属
        supplierBusinessDetail.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierBusinessDetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierBusinessDetailEditParam supplierBusinessDetailEditParam) {
        SupplierBusinessDetail supplierBusinessDetail = this.queryEntity(supplierBusinessDetailEditParam.getId());
        BeanUtil.copyProperties(supplierBusinessDetailEditParam, supplierBusinessDetail);
        this.updateById(supplierBusinessDetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierBusinessDetailIdParam> supplierBusinessDetailIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierBusinessDetailIdParamList, SupplierBusinessDetailIdParam::getId));
    }

    @Override
    public SupplierBusinessDetail detail(SupplierBusinessDetailIdParam supplierBusinessDetailIdParam) {
        return this.queryEntity(supplierBusinessDetailIdParam.getId());
    }

    @Override
    public SupplierBusinessDetail queryEntity(String id) {
        SupplierBusinessDetail supplierBusinessDetail = this.getById(id);
        if (ObjectUtil.isEmpty(supplierBusinessDetail)) {
            throw new CommonException("业务明细表不存在，id值为：{}", id);
        }
        return supplierBusinessDetail;
    }

    @Override
    public List<SupplierBusinessDetail> detailList(SupplierBusinessDetailPageParam supplierBusinessDetailPageParam) {
        QueryWrapper<SupplierBusinessDetail> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(supplierBusinessDetailPageParam.getDetailBusinessId())) { //主业务ID
            queryWrapper.lambda().eq(SupplierBusinessDetail::getDetailBusinessId, supplierBusinessDetailPageParam.getDetailBusinessId());
        }
        if (ObjectUtil.isNotEmpty(supplierBusinessDetailPageParam.getDetailProductionId())) {  //产品ID
            queryWrapper.lambda().eq(SupplierBusinessDetail::getDetailProductionId, supplierBusinessDetailPageParam.getDetailProductionId());
        }
        if (ObjectUtil.isNotEmpty(supplierBusinessDetailPageParam.getDetailShippingStorage())) { //出库仓库
            queryWrapper.lambda().eq(SupplierBusinessDetail::getDetailShippingStorage, supplierBusinessDetailPageParam.getDetailShippingStorage());
        }
        if (ObjectUtil.isNotEmpty(supplierBusinessDetailPageParam.getDetailStorageStorage())) {   //入库仓库
            queryWrapper.lambda().eq(SupplierBusinessDetail::getDetailStorageStorage, supplierBusinessDetailPageParam.getDetailStorageStorage());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierBusinessDetail::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());

        if (ObjectUtil.isAllNotEmpty(supplierBusinessDetailPageParam.getSortField(), supplierBusinessDetailPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierBusinessDetailPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierBusinessDetailPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierBusinessDetailPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierBusinessDetail::getId);
        }
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateDetail(SupplierBusinessMain supplierBusinessMainOld, SupplierBusinessMain supplierBusinessMainNew, List<SupplierBusinessDetail> supplierBusinessDetailList) {
        //保存明细业务数据
        this.saveOrUpdateBatch(supplierBusinessDetailList);
        //把留下的保存，没有的删除
        List<String> detailIdsOld = CollStreamUtil.toList(this.list(new LambdaQueryWrapper<SupplierBusinessDetail>().eq(SupplierBusinessDetail::getDetailBusinessId, supplierBusinessMainNew.getId()).eq(SupplierBusinessDetail::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId())), SupplierBusinessDetail::getId);
        List<String> detailIds = CollStreamUtil.toList(supplierBusinessDetailList, SupplierBusinessDetail::getId);
        if (ObjectUtil.isNotEmpty(detailIds)) {
            detailIdsOld.removeAll(detailIds);
        }
        //删除已经不在的明细数据
        this.removeByIds(detailIdsOld);

        //翻译明细数据列表
        transService.transBatch(supplierBusinessDetailList);
        //如果原单据为空（说明是直接审核过账的）或原单据状态为草稿，新单据为过账（此为审核过账动作）则执行审核过账动作。
        if (supplierBusinessMainNew.getBusinessStatus().equals("1") && (ObjectUtil.isEmpty(supplierBusinessMainOld) || supplierBusinessMainOld.getBusinessStatus().equals("0"))) {
            List<SupplierStoresStream> supplierStoresStreamList = new ArrayList<>();//库存流水列表待保存
            List<SupplierStocks> supplierStocksList = new ArrayList<>();//库存明细列表待保存
            switch (supplierBusinessMainNew.getBusinessType()) {
                case "EN": //采购入库
                    {
                        //处理明细数据，包括：处理库存流水，库存明细，总库存和移动加权平均成本价，
                        supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                            //1、处理库存流水
                            SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                            supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                            supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                            supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                            supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                            supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                            supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                            supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                            supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessStorageStoreId()); //入库门店
                            supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//入库仓库
                            supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//入库库位
                            supplierStoresStream.setStreamInNumber(supplierBusinessDetail.getDetailNumberTotal());//入库数量，含赠送
                            supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                            supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                            supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()));//实时库存，入库后库存
                            supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                            supplierStoresStream.setRemark(supplierBusinessDetail.getDetailRemark());//备注
                            supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                            supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                            //2、处理库存明细(先查找与本次操作的产品)
                            SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                            SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                            LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStoreId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessStorageStoreId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStorageId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessStorageStorageId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessObjectId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockSupplier, supplierBusinessMainNew.getBusinessObjectId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                            }
                            List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                            if (ObjectUtil.isNotEmpty(supplierStockss)) {
                                supplierStocks = supplierStockss.get(0);
                                BigDecimal newStockStock = supplierStocks.getStockStock().add(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存减掉当前退库的数量
                                BigDecimal newStockPrice = newStockStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure())).add(supplierBusinessDetail.getDetailAmount())).divide(newStockStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                                supplierStocks.setStockStock(newStockStock);//把库存明细的库存加上去
                                supplierStocks.setStockPriceProcure(newStockPrice);//采购价采用移动加权平均法处理
                                supplierStocks.setStockPriceProcureLast(supplierBusinessDetail.getDetailPrice());//最后一次采购单价
                                supplierStocks.setStockAmount(newStockStock.multiply(newStockPrice));//库存金额
                            } else {
                                supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessStorageStoreId());//库存明细门店
                                supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//库存明细仓库
                                supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                                supplierStocks.setStockPriceProcure(supplierBusinessDetail.getDetailPrice());//采购单价
                                supplierStocks.setStockPriceProcureLast(supplierBusinessDetail.getDetailPrice());//最后一次采购单价
                                supplierStocks.setStockStock(supplierBusinessDetail.getDetailNumberTotal());//库存明细库存
                                supplierStocks.setStockAmount(supplierBusinessDetail.getDetailAmount());//库存金额
                                supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                                supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                                supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                                supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                                supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                            }
                            supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                            //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                            BigDecimal newStock = supplierProductions.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数
                            BigDecimal newPrice = newStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierProductions.getProductionStock().multiply(supplierProductions.getProductionPriceProcure())).add(supplierBusinessDetail.getDetailAmount())).divide(newStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额+新金额）/ 新数量
                            supplierProductions.setProductionPriceProcure(newPrice);
                            supplierProductions.setProductionPriceProcureLast(supplierBusinessDetail.getDetailPrice());//更新最近一次采购价
                            supplierProductions.setProductionStock(newStock);//把商品库的库存加上去
                            supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                        });
                        break;
                    }
                case "RE"://采购退货
                    {
                        supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                            //1、处理库存流水
                            SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                            supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                            supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                            supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                            supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                            supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                            supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                            supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                            supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessShippingStoreId()); //出库门店
                            supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessShippingStorageId());//出库仓库
                            supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//出库库位
                            supplierStoresStream.setStreamOutNumber(supplierBusinessDetail.getDetailNumberTotal());//出库库数量，含赠送
                            supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                            supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                            supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().subtract(supplierBusinessDetail.getDetailNumberTotal()));//实时库存，出库后库存
                            supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                            supplierStoresStream.setRemark(supplierBusinessDetail.getDetailRemark());//备注
                            supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                            supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                            //2、处理库存明细
                            SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                            SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                            LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessShippingStoreId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessShippingStoreId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessShippingStorageId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessShippingStorageId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessObjectId())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockSupplier, supplierBusinessMainNew.getBusinessObjectId());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                            }
                            if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                                lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                            }
                            List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                            if (ObjectUtil.isNotEmpty(supplierStockss)) {
                                supplierStocks = supplierStockss.get(0);
                                BigDecimal newStockStock = supplierStocks.getStockStock().subtract(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存减掉当前退库的数量
                                BigDecimal newStockPrice = newStockStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure())).subtract(supplierBusinessDetail.getDetailAmount())).divide(newStockStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                                supplierStocks.setStockStock(newStockStock);//把库存明细的库存加上去
                                supplierStocks.setStockPriceProcure(newStockPrice);//采购价采用移动加权平均法处理
                                supplierStocks.setStockAmount(newStockStock.multiply(newStockPrice));//库存金额
                            } else {
                                supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessShippingStoreId());//库存明细门店
                                supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessShippingStorageId());//库存明细仓库
                                supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                                supplierStocks.setStockPriceProcure(supplierBusinessDetail.getDetailPrice());//采购单价
                                supplierStocks.setStockPriceProcureLast(supplierBusinessDetail.getDetailPrice());//最后一次采购单价
                                supplierStocks.setStockStock(BigDecimal.ZERO.subtract(supplierBusinessDetail.getDetailNumberTotal()));//库存明细负库存
                                supplierStocks.setStockAmount(BigDecimal.ZERO.subtract(supplierBusinessDetail.getDetailAmount()));//负金额
                                supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                                supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                                supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                                supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                                supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属

                            }
                            supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                            //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                            BigDecimal newStock = supplierProductions.getProductionStock().subtract(supplierBusinessDetail.getDetailNumberTotal()); //出库后的新库存数
                            BigDecimal newPrice = newStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierProductions.getProductionStock().multiply(supplierProductions.getProductionPriceProcure())).subtract(supplierBusinessDetail.getDetailAmount())).divide(newStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-退货金额）/ 新数量
                            supplierProductions.setProductionPriceProcure(newPrice);
//                            supplierProductions.setProductionPriceProcureLast(supplierBusinessDetail.getDetailPrice());//更新最近一次采购价
                            supplierProductions.setProductionStock(newStock);//更新商品库的库存
                            supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                        });
                        break;
                    }
                case "SO"://销售出库
                {
                    supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                        //1、处理库存流水
                        SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                        supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                        supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                        supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                        supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                        supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                        supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                        supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                        supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                        supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessShippingStoreId()); //出库门店
                        supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessShippingStorageId());//出库仓库
                        supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//出库库位
                        supplierStoresStream.setStreamOutNumber(supplierBusinessDetail.getDetailNumberTotal());//出库库数量，含赠送
                        supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                        supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                        supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().subtract(supplierBusinessDetail.getDetailNumberTotal()));//实时库存，出库后库存
                        supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                        supplierStoresStream.setRemark(supplierBusinessDetail.getDetailRemark());//备注
                        supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                        //2、处理库存明细
                        SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                        SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                        LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessShippingStoreId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessShippingStoreId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessShippingStorageId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessShippingStorageId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                        }
                        List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                        if (ObjectUtil.isNotEmpty(supplierStockss)) {
                            supplierStocks = supplierStockss.get(0);
                            supplierStocks.setStockStock(supplierStocks.getStockStock().subtract(supplierBusinessDetail.getDetailNumberTotal()));//把库存明细的库存减掉
                            supplierStocks.setStockAmount(supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure()));//新库存乘成本
                        } else {
                            supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessShippingStoreId());//库存明细门店
                            supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessShippingStorageId());//库存明细仓库
                            supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                            supplierStocks.setStockStock(BigDecimal.ZERO.subtract(supplierBusinessDetail.getDetailNumberTotal()));//库存明细负库存
                            supplierStocks.setStockAmount(supplierStocks.getStockStock().multiply(supplierProductions.getProductionPriceProcure()));//库存乘成本
                            supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                            supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                            supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                            supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        }
                        supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                        //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                        BigDecimal newStock = supplierProductions.getProductionStock().subtract(supplierBusinessDetail.getDetailNumberTotal()); //出库后的新库存数
                        supplierProductions.setProductionStock(newStock);//更新商品库的库存
                        supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                    });
                    break;
                }
                case "RS"://销售退货
                {
                    supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                        //1、处理库存流水
                        SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                        supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                        supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                        supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                        supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                        supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                        supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                        supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                        supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                        supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessStorageStoreId()); //入库门店
                        supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//入库仓库
                        supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//入库库位
                        supplierStoresStream.setStreamInNumber(supplierBusinessDetail.getDetailNumberTotal());//退库数量，含赠送
                        supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                        supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                        supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()));//实时库存，出库后库存
                        supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                        supplierStoresStream.setRemark(supplierBusinessDetail.getDetailRemark());//备注
                        supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                        //2、处理库存明细
                        SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                        SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                        LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStoreId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessStorageStoreId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStorageId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessStorageStorageId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                        }
                        List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                        if (ObjectUtil.isNotEmpty(supplierStockss)) {
                            supplierStocks = supplierStockss.get(0);
                            supplierStocks.setStockStock(supplierStocks.getStockStock().add(supplierBusinessDetail.getDetailNumberTotal()));//把库存明细的库存减掉
                            supplierStocks.setStockAmount(supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure()));//新库存乘成本
                        } else {
                            supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessStorageStoreId());//库存明细门店
                            supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//库存明细仓库
                            supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                            supplierStocks.setStockStock(supplierBusinessDetail.getDetailNumberTotal());//库存明细负库存
                            supplierStocks.setStockAmount(supplierStocks.getStockStock().multiply(supplierProductions.getProductionPriceProcure()));//库存乘成本
                            supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                            supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                            supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                            supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        }
                        supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                        //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                        BigDecimal newStock = supplierProductions.getProductionStock().subtract(supplierBusinessDetail.getDetailNumberTotal()); //出库后的新库存数
                        supplierProductions.setProductionStock(newStock);//更新商品库的库存
                        supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                    });
                    break;
                }
                default:
                    break;

            }
            supplierStoresStreamService.saveOrUpdateBatch(supplierStoresStreamList);//批量保存库存流水
            supplierStocksService.saveOrUpdateBatch(supplierStocksList);//批量保存库存明细
        }
        //反审核流程
        if (supplierBusinessMainNew.getBusinessStatus().equals("0") && (ObjectUtil.isNotEmpty(supplierBusinessMainOld) && supplierBusinessMainOld.getBusinessStatus().equals("1"))) {
            //Todo
            List<SupplierStoresStream> supplierStoresStreamList = new ArrayList<>();//库存流水列表待保存
            List<SupplierStocks> supplierStocksList = new ArrayList<>();//库存明细列表待保存
            switch (supplierBusinessMainNew.getBusinessType()) {
                case "EN": //采购入库
                {
                    //处理明细数据，包括：处理库存流水，库存明细，总库存和移动加权平均成本价，
                    supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                        //1、处理库存流水
                        SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                        supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                        supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                        supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                        supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                        supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                        supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                        supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                        supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                        supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessStorageStoreId()); //入库门店
                        supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//入库仓库
                        supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//入库库位
                        supplierStoresStream.setStreamInNumber(BigDecimal.ZERO.subtract(supplierBusinessDetail.getDetailNumberTotal()));//0-入库数量，含赠送
                        supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                        supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                        supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().add(BigDecimal.ZERO.subtract(supplierBusinessDetail.getDetailNumberTotal())));//实时库存，退库后库存
                        supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                        supplierStoresStream.setRemark("反审核单据!" + (ObjectUtil.isEmpty(supplierBusinessDetail.getDetailRemark()) ? "" : supplierBusinessDetail.getDetailRemark()));//备注
                        supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                        //2、处理库存明细(先查找与本次操作的产品)
                        SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                        SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                        LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStoreId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessStorageStoreId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStorageId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessStorageStorageId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessObjectId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockSupplier, supplierBusinessMainNew.getBusinessObjectId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                        }
                        List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                        if (ObjectUtil.isNotEmpty(supplierStockss)) {
                            supplierStocks = supplierStockss.get(0);
                            BigDecimal newStockStock = supplierStocks.getStockStock().subtract(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存减掉当前退库的数量
                            BigDecimal newStockPrice = newStockStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure())).subtract(supplierBusinessDetail.getDetailAmount())).divide(newStockStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                            supplierStocks.setStockStock(newStockStock);//把库存明细的库存加上去
                            supplierStocks.setStockPriceProcure(newStockPrice);//采购价采用移动加权平均法处理
                            supplierStocks.setStockAmount(newStockStock.multiply(newStockPrice));//库存金额
                        } else {
                            supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessStorageStoreId());//库存明细门店
                            supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//库存明细仓库
                            supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                            supplierStocks.setStockStock(BigDecimal.ZERO.subtract(supplierBusinessDetail.getDetailNumberTotal()));//负库存
                            supplierStocks.setStockPriceProcure(supplierBusinessDetail.getDetailPrice());
                            supplierStocks.setStockAmount(BigDecimal.ZERO.subtract(supplierBusinessDetail.getDetailAmount()));
                            supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                            supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                            supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                            supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        }
                        supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                        //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                        BigDecimal newStock = supplierProductions.getProductionStock().subtract(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存减掉当前退库的数量
                        BigDecimal newPrice = newStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierProductions.getProductionStock().multiply(supplierProductions.getProductionPriceProcure())).subtract(supplierBusinessDetail.getDetailAmount())).divide(newStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                        supplierProductions.setProductionPriceProcure(newPrice);
                        //supplierProductions.setProductionPriceProcureLast(supplierBusinessDetail.getDetailPrice());//更新最近一次采购价 --这个是不好回滚了
                        supplierProductions.setProductionStock(newStock);//把商品库的库存更新
                        supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                    });
                    break;
                }
                case "RE"://采购退货
                {
                    //处理明细数据，包括：处理库存流水，库存明细，总库存和移动加权平均成本价，
                    supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                        //1、处理库存流水
                        SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                        supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                        supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                        supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                        supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                        supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                        supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                        supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                        supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                        supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessStorageStoreId()); //入库门店
                        supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//入库仓库
                        supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//入库库位
                        supplierStoresStream.setStreamInNumber(supplierBusinessDetail.getDetailNumberTotal());//0-入库数量，含赠送
                        supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                        supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                        supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()));//实时库存，退库后库存
                        supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                        supplierStoresStream.setRemark("反审核单据!" + (ObjectUtil.isEmpty(supplierBusinessDetail.getDetailRemark()) ? "" : supplierBusinessDetail.getDetailRemark()));//备注
                        supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                        //2、处理库存明细(先查找与本次操作的产品)
                        SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                        SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                        LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStoreId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessStorageStoreId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStorageId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessStorageStorageId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessObjectId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockSupplier, supplierBusinessMainNew.getBusinessObjectId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                        }
                        List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                        if (ObjectUtil.isNotEmpty(supplierStockss)) {
                            supplierStocks = supplierStockss.get(0);
                            BigDecimal newStockStock = supplierStocks.getStockStock().add(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存减掉当前退库的数量
                            BigDecimal newStockPrice = newStockStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure())).add(supplierBusinessDetail.getDetailAmount())).divide(newStockStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                            supplierStocks.setStockStock(newStockStock);//把库存明细的库存加上去
                            supplierStocks.setStockPriceProcure(newStockPrice);//采购价采用移动加权平均法处理
                            supplierStocks.setStockAmount(newStockStock.multiply(newStockPrice));//库存金额
                        } else {
                            supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessStorageStoreId());//库存明细门店
                            supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//库存明细仓库
                            supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                            supplierStocks.setStockStock(supplierBusinessDetail.getDetailNumberTotal());//负库存
                            supplierStocks.setStockPriceProcure(supplierBusinessDetail.getDetailPrice());
                            supplierStocks.setStockAmount(supplierBusinessDetail.getDetailAmount());
                            supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                            supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                            supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                            supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        }
                        supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                        //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                        BigDecimal newStock = supplierProductions.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存加上当前退库的数量
                        BigDecimal newPrice = newStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierProductions.getProductionStock().multiply(supplierProductions.getProductionPriceProcure())).subtract(supplierBusinessDetail.getDetailAmount())).divide(newStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                        supplierProductions.setProductionPriceProcure(newPrice);
                        supplierProductions.setProductionStock(newStock);//把商品库的库存更新
                        supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                    });
                    break;
                }
                case "SO"://销售出库
                {
                    //处理明细数据，包括：处理库存流水，库存明细，总库存和移动加权平均成本价，
                    supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                        transService.transOne(supplierBusinessDetail);
                        //1、处理库存流水
                        SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                        supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                        supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                        supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                        supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                        supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                        supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                        supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                        supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                        supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessShippingStoreId()); //出库门店
                        supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessShippingStorageId());//出库仓库
                        supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//出库库位
                        supplierStoresStream.setStreamInNumber(supplierBusinessDetail.getDetailNumberTotal());//0-入库数量，含赠送
                        supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                        supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                        supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()));//实时库存，退库后库存
                        supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                        supplierStoresStream.setRemark("反审核单据!" + (ObjectUtil.isEmpty(supplierBusinessDetail.getDetailRemark()) ? "" : supplierBusinessDetail.getDetailRemark()));//备注
                        supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                        //2、处理库存明细(先查找与本次操作的产品)
                        SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                        SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                        LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessShippingStoreId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessShippingStoreId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessShippingStorageId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessShippingStorageId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                        }
                        List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                        if (ObjectUtil.isNotEmpty(supplierStockss)) {
                            supplierStocks = supplierStockss.get(0);
                            supplierStocks.setStockStock(supplierStocks.getStockStock().add(supplierBusinessDetail.getDetailNumberTotal()));//把库存明细的从库存上减掉
                            supplierStocks.setStockAmount(supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure()));
                        } else {
                            supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessShippingStoreId());//库存明细门店
                            supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessShippingStorageId());//库存明细仓库
                            supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                            supplierStocks.setStockStock(supplierBusinessDetail.getDetailNumberTotal());//负库存
                            supplierStocks.setStockAmount(supplierBusinessDetail.getProductionPriceProcure().multiply(supplierBusinessDetail.getDetailNumberTotal()));
                            supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                            supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                            supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                            supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        }
                        supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                        //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                        BigDecimal newStock = supplierProductions.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存加上当前退库的数量
                        BigDecimal newPrice = newStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierProductions.getProductionStock().multiply(supplierProductions.getProductionPriceProcure())).subtract(supplierBusinessDetail.getDetailAmount())).divide(newStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                        supplierProductions.setProductionPriceProcure(newPrice);
                        supplierProductions.setProductionStock(newStock);//把商品库的库存更新
                        supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                    });
                    break;
                }
                case "RS"://销售退货
                {
                    //处理明细数据，包括：处理库存流水，库存明细，总库存和移动加权平均成本价，
                    supplierBusinessDetailList.forEach((SupplierBusinessDetail supplierBusinessDetail) -> {
                        transService.transOne(supplierBusinessDetail);
                        //1、处理库存流水
                        SupplierStoresStream supplierStoresStream = new SupplierStoresStream();
                        supplierStoresStream.setStreamProductionId(supplierBusinessDetail.getDetailProductionId());//商品ID
                        supplierStoresStream.setStreamProductionCode(supplierBusinessDetail.getProductionCode());//商品编码
                        supplierStoresStream.setStreamProductionBarcode(supplierBusinessDetail.getProductionBarcode());//商品条码
                        supplierStoresStream.setStreamProductionName(supplierBusinessDetail.getProductionName());//商品名称
                        supplierStoresStream.setStreamProductionSpec(supplierBusinessDetail.getProductionSpec());//商品规格
                        supplierStoresStream.setStreamProductionUnit(supplierBusinessDetail.getProductionUnit());//商品单位
                        supplierStoresStream.setStreamProductionBatch(supplierBusinessDetail.getDetailProductionBatch());//批次
                        supplierStoresStream.setStreamDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                        supplierStoresStream.setStreamStoreId(supplierBusinessMainNew.getBusinessStorageStoreId()); //入库门店
                        supplierStoresStream.setStreamStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//入库仓库
                        supplierStoresStream.setStreamStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//入库库位
                        supplierStoresStream.setStreamOutNumber(supplierBusinessDetail.getDetailNumberTotal());//0-入库数量，含赠送
                        supplierStoresStream.setStreamBusinessType(supplierBusinessMainNew.getBusinessType());//业务类型
                        supplierStoresStream.setStreamBusinessId(supplierBusinessMainNew.getId());//主业务单ID
                        supplierStoresStream.setStreamProductionStock(supplierBusinessDetail.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()));//实时库存，退库后库存
                        supplierStoresStream.setStreamObjectId(supplierBusinessMainNew.getBusinessObjectId());//业务对象ID
                        supplierStoresStream.setRemark("反审核单据!" + (ObjectUtil.isEmpty(supplierBusinessDetail.getDetailRemark()) ? "" : supplierBusinessDetail.getDetailRemark()));//备注
                        supplierStoresStream.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        supplierStoresStreamList.add(supplierStoresStream);//添加到库存流水列表待保存
                        //2、处理库存明细(先查找与本次操作的产品)
                        SupplierStocks supplierStocks = new SupplierStocks();//库存明细
                        SupplierProductions supplierProductions = supplierProductionsService.queryEntity(supplierBusinessDetail.getDetailProductionId());//产品库
                        LambdaQueryWrapper<SupplierStocks> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getCompanyId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getCompanyId, supplierBusinessMainNew.getCompanyId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStoreId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreId, supplierBusinessMainNew.getBusinessStorageStoreId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessMainNew.getBusinessStorageStorageId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStorageId, supplierBusinessMainNew.getBusinessStorageStorageId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionId())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockProductionId, supplierBusinessDetail.getDetailProductionId());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailProductionBatch())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockBatch, supplierBusinessDetail.getDetailProductionBatch());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailDateManufacture())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockDateManufacture, supplierBusinessDetail.getDetailDateManufacture());
                        }
                        if(ObjectUtil.isNotEmpty(supplierBusinessDetail.getDetailStorageLocation())) {
                            lambdaQueryWrapper.eq(SupplierStocks::getStockStoreLocation, supplierBusinessDetail.getDetailStorageLocation());
                        }
                        List<SupplierStocks> supplierStockss = supplierStocksService.list(lambdaQueryWrapper);
                        if (ObjectUtil.isNotEmpty(supplierStockss)) {
                            supplierStocks = supplierStockss.get(0);
                            supplierStocks.setStockStock(supplierStocks.getStockStock().subtract(supplierBusinessDetail.getDetailNumberTotal()));//把库存明细的从库存上减掉
                            supplierStocks.setStockAmount(supplierStocks.getStockStock().multiply(supplierStocks.getStockPriceProcure()));
                        } else {
                            supplierStocks.setStockStoreId(supplierBusinessMainNew.getBusinessStorageStoreId());//库存明细门店
                            supplierStocks.setStockStorageId(supplierBusinessMainNew.getBusinessStorageStorageId());//库存明细仓库
                            supplierStocks.setStockProductionId(supplierBusinessDetail.getDetailProductionId());//库存明细商品ID
                            supplierStocks.setStockStock(supplierBusinessDetail.getDetailNumberTotal());//负库存
                            supplierStocks.setStockAmount(supplierBusinessDetail.getDetailNumberTotal().multiply(supplierBusinessDetail.getProductionPriceProcure()));
                            supplierStocks.setStockStoreLocation(supplierBusinessDetail.getDetailStorageLocation());//库存明细库位
                            supplierStocks.setStockBatch(supplierBusinessDetail.getDetailProductionBatch());//库存明细批次
                            supplierStocks.setStockDateManufacture(supplierBusinessDetail.getDetailDateManufacture());//生产日期
                            supplierStocks.setStockSupplier(supplierBusinessMainNew.getBusinessObjectId());//库存明细供应商
                            supplierStocks.setCompanyId(supplierBusinessMainNew.getCompanyId());//数据归属
                        }
                        supplierStocksList.add(supplierStocks);//添加库存明细列表待保存
                        //3、处理商品库里的信息，包括总库存和移动加权平均的成本价
                        BigDecimal newStock = supplierProductions.getProductionStock().add(supplierBusinessDetail.getDetailNumberTotal()); //新的库存数=原库存加上当前退库的数量
                        BigDecimal newPrice = newStock.compareTo(BigDecimal.ZERO)== 0 ? BigDecimal.ZERO : ((supplierProductions.getProductionStock().multiply(supplierProductions.getProductionPriceProcure())).subtract(supplierBusinessDetail.getDetailAmount())).divide(newStock, new MathContext(2, RoundingMode.HALF_UP));//（原金额-新金额）/ 新数量
                        supplierProductions.setProductionPriceProcure(newPrice);
                        supplierProductions.setProductionStock(newStock);//把商品库的库存更新
                        supplierProductionsService.saveOrUpdate(supplierProductions);//立即保存，否则当同一商品在一张单据中多次出现时，库存就不准了
                    });
                    break;
                }
                default:
                    break;

            }

            supplierStoresStreamService.saveOrUpdateBatch(supplierStoresStreamList);//批量保存库存流水
            supplierStocksService.saveOrUpdateBatch(supplierStocksList);//批量保存库存明细
        }
    }
}